package designPatterns.createMode.prototype;

/**
 * Created by thinkpad on 2015/7/12.
 * 原型模式被用在频繁调用且极其相似的对象上，它会克隆对象并设置改变后的属性，而且消耗的资源较少。
 */
//原型
interface Prototype {
    void setSize(int x);
    void printSize();
}

// 一个具体类
class A implements Prototype, Cloneable {
    private int size;

    public A(int x) {
        this.size = x;
    }

    @Override
    public void setSize(int x) {
        this.size = x;
    }

    @Override
    public void printSize() {
        System.out.println("Size: " + size);
    }

    @Override
    public A clone() throws CloneNotSupportedException {
        return (A) super.clone();
    }
}

//需要很多类似的对象进行测试
public class PrototypeTest {
    public static void main(String args[]) throws CloneNotSupportedException {
        A a = new A(1);

        for (int i = 2; i < 10; i++) {
            Prototype temp = a.clone();
            temp.setSize(i);
            temp.printSize();
        }
    }
}

